package com.nlx.notes.module.service.impl.gold;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nlx.notes.module.bean.dto.gold.GoldPurchaseAddDTO;
import com.nlx.notes.module.bean.dto.gold.GoldPurchasePageDTO;
import com.nlx.notes.module.bean.dto.gold.GoldPurchaseSelectListDTO;
import com.nlx.notes.module.bean.vo.PageResultVo;
import com.nlx.notes.module.bean.vo.Pagination;
import com.nlx.notes.module.bean.vo.gold.GoldPurchaseBaseInfoVO;
import com.nlx.notes.module.bean.vo.gold.GoldPurchasePageVO;
import com.nlx.notes.module.core.consts.GlobalConst;
import com.nlx.notes.module.core.enums.error.GoldCodeEnum;
import com.nlx.notes.module.core.util.DataConvertorUtils;
import com.nlx.notes.module.entity.GoldPurchaseEntity;
import com.nlx.notes.module.mapper.GoldPurchaseServiceMapper;
import com.nlx.notes.module.service.gold.IGoldPurchaseService;
import com.nlx.notes.support.handler.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Slf4j
@Service
public class GoldPurchaseServiceImpl extends ServiceImpl<GoldPurchaseServiceMapper, GoldPurchaseEntity> implements IGoldPurchaseService {


    @Override
    public long countByCondition(GoldPurchasePageDTO goldPurchasePageDTO) {
        return this.getBaseMapper().countByCondition(goldPurchasePageDTO);
    }

    @Override
    public List<GoldPurchaseEntity> pageByCondition(GoldPurchasePageDTO goldPurchasePageDTO) {
        return this.getBaseMapper().pageByCondition(goldPurchasePageDTO);
    }

    @Override
    public List<GoldPurchaseEntity> selectListByCondition(GoldPurchaseSelectListDTO goldPurchaseSelectListDTO) {
        return this.getBaseMapper().selectListByCondition(goldPurchaseSelectListDTO);
    }

    @Override
    public GoldPurchaseEntity findById(Long goldId, Long userId) {
        if(null == goldId || null == userId){
            return null;
        }
        return this.getBaseMapper().selectOne(Wrappers.lambdaQuery(GoldPurchaseEntity.class)
                .eq(GoldPurchaseEntity::getId,goldId)
                .eq(GoldPurchaseEntity::getUserId,userId)
                .eq(GoldPurchaseEntity::getDelFlag,GlobalConst.DEL_FLAG_NORMAL)
                .last(" limit 1"));
    }

    @Override
    public BigDecimal sumWeightByUser(Long userId) {
        if( null == userId){
            return BigDecimal.ZERO;
        }
        return this.getBaseMapper().sumWeightByUser(userId);
    }

    @Override
    public BigDecimal sumTotalPriceByUser(Long userId) {
        if( null == userId){
            return BigDecimal.ZERO;
        }
        return this.getBaseMapper().sumTotalPriceByUser(userId);
    }

    @Override
    public boolean removeGold(Long goldId, Long userId) {
        if(null == goldId || null == userId){
            return false;
        }
        return this.getBaseMapper().update(Wrappers.lambdaUpdate(GoldPurchaseEntity.class)
                .set(GoldPurchaseEntity::getDelFlag,GlobalConst.DEL_FLAG_REMOVE)
                .eq(GoldPurchaseEntity::getId,goldId)
                .eq(GoldPurchaseEntity::getUserId,userId)) > 0;
    }
}
